﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Cailiao
{
    /// <summary>
    /// ListPage.xaml 的交互逻辑
    /// </summary>
    public partial class ListPage : Page
    {
        ViewModel.MaterialViewModel vm;
        Model.DBContext dBContext = new Model.DBContext();

        public ListPage(bool IsAll =false)
        {
            InitializeComponent();

            vm = DataContext as ViewModel.MaterialViewModel;
            if(IsAll)
            {
                vm.BillList = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.State != "待审核").ToList();
            }
            else
            {
                vm.BillList = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.UserId == Comm.Temp.LoginUser.Id).ToList();
            }
          

            dp_Start.SelectedDate = DateTime.Now.Date;
            dp_End.SelectedDate = DateTime.Now.AddDays(1).Date;


            vm.depList = dBContext.DB.Queryable<Model.Depment>().ToList();
        }

        private void Btn_select_Click(object sender, RoutedEventArgs e)
        {
            
            var query = dBContext.DB.Queryable<Model.MaterialBill>();
            if(!string.IsNullOrWhiteSpace(txt_User.Text))
            {
                query = query.Where(m=>m.UserName.Contains(txt_User.Text));
            }

            if( cmb_depname.SelectedValue!=null)
            {
                query = query.Where(m => m.DepId == (int)cmb_depname.SelectedValue);
                 
            }

            if(dp_Start.SelectedDate !=null)
            {
                
                query = query.Where(m => m.CreateTime >= dp_Start.SelectedDate);
            }

            if(dp_End.SelectedDate != null)
            {
               
                query = query.Where(m => m.CreateTime < dp_End.SelectedDate);
            }
            var CurrRole = dBContext.DB.Queryable<Model.Roles>().Where(r => r.Id == Comm.Temp.LoginUser.RoleId).First();
            if (!CurrRole.FuncCailiao)
            {
                query = query.Where(r => r.UserId == Comm.Temp.LoginUser.Id);
            }

          
           vm.BillList = query.ToList();

        }

        private void Btn_Clear_Click(object sender, RoutedEventArgs e)
        {
            dp_Start.SelectedDate = DateTime.Now.Date;
            dp_End.SelectedDate = DateTime.Now.AddDays(1).Date;
            vm.BillList = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.State != "待审核").ToList();
        }

       

        private void btn_Tongguo(object sender, RoutedEventArgs e)
        {
            vm.BillList = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.State == "已通过").ToList();
        }

        private void btn_Bohui(object sender, RoutedEventArgs e)
        {
            vm.BillList = dBContext.DB.Queryable<Model.MaterialBill>().Where(m => m.State == "已驳回").ToList();
        }
    }
}
